package com.wcl.demo.db;

import android.content.ContentValues;

import com.wcl.demo.model.entity.DaoSession;
import com.wcl.demo.model.entity.InviteMessage;
import com.wcl.demo.model.entity.InviteMessageDao;
import com.wcl.demo.model.entity.InviteMessages;
import com.wcl.demo.model.entity.InviteMessagesDao;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/11/24.
 */
public class MessageHelper {
    private DaoSingleton instance = DaoSingleton.getInstance();
    private InviteMessageDao messageDao;
    private DaoSession daoSession;

    public MessageHelper() {
        messageDao = instance.getMessageDao();
        daoSession=instance.getDaoSession();
    }

    /***
     * 保存申请邀请信息
     *
     * @param messages
     */
    public void saveMessage(InviteMessage message) {
        messageDao.insertInTx(message);
    }

    /***
     * 删除申请邀请信息
     */
    public void deleteMessage() {
        messageDao.deleteAll();
    }

    /***
     * 查询
     */

    public List<InviteMessage> queryMessage() {
        return messageDao.loadAll();
    }

    /***
     * 更新消息
     *
     * @param messages
     */
//    public void updateMessage(InviteMessage messages) {
//        messageDao.update(messages);
//    }


    /**
     * 数据中是否存在该用户
     * @param name
     * @return
     */
    public boolean getName(String username) {
        QueryBuilder<InviteMessage> qb = daoSession.getInviteMessageDao().queryBuilder();
        qb.where(InviteMessageDao.Properties.From.eq(username));

        return qb.buildCount().count() > 0 ? true : false;

    }

    public  void updateMessage(InviteMessage message) {
        messageDao.insertOrReplace(message);
    }

    /**
     * 根据username查找
     * @param username
     * @return
     */
    public List<InviteMessage> queryUser(String username) {

        QueryBuilder<InviteMessage> qb = daoSession.getInviteMessageDao().queryBuilder();
        qb.where(InviteMessageDao.Properties.From.eq(username));
        if (qb.list().size() > 0) {
            return qb.list();
        } else {
            return null;
        }
    }

}
